home *** CD-ROM | disk | FTP | other *** search
/ ADA Programming Guide / ADA Programming Guide.iso / ada_gwu / load.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-01-30  |  3.8 KB  |  146 lines

  1. /*
  2.     GWAda Development Environment for 386/486 PCs   
  3.     Copyright (C) 1993, Arthur Vargas Lopes  & Michael Bliss Feldman
  4.                         vlopes@vortex.ufrgs.br mfeldman@seas.gwu.edu
  5.  
  6.     This program is free software; you can redistribute it and/or modify
  7.     it under the terms of the GNU General Public License as published by
  8.     the Free Software Foundation; version 2 of the License.    
  9.  
  10.  
  11.     This program is distributed in the hope that it will be useful,
  12.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.     GNU General Public License for more details.
  15.  
  16.     You should have received a copy of the GNU General Public License
  17.     along with this program; if not, write to the Free Software
  18.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. */
  20.  
  21. /* load.c */
  22.  
  23. #include <\gwada\env\externs.h>
  24.  
  25.  
  26.  
  27.  
  28. void AVL_PROCESS_LOAD() 
  29. {
  30.     AVL_EDIT_WINDOW_PTR w;
  31.     AVL_WIN_PTR m1;
  32.     int n1, n2;
  33.     static char *msg = " GWAda - Enter file name to load, please: ";
  34.     char f[61];
  35.     w = &avl_windows[avl_window];
  36.     n1 = strlen(msg) + 10;
  37.     if (n1 < 62) n1 = 62;
  38.     n2 = (80 - n1) / 2;
  39.     m1 = AVL_MAKE_WINDOW(msg,7,n2,9,n1+n2,avl_wnd_bk_color,avl_wnd_color);
  40.     _settextposition(1,1);
  41.     f[0] = '\0';
  42.     AVL_PROMPT(1,1,f,60);
  43.     AVL_DEL_WINDOW(m1);
  44.     if (strlen(f) > 0) {
  45.         AVL_FREE_ALL();
  46.         AVL_LOAD_FILE(f);
  47.         }    
  48. }
  49.  
  50.  
  51.  
  52. void AVL_LOAD_FILE(char *fn)
  53. {
  54.         AVL_LINE_PTR head = NULL, temp;
  55.         int x ;
  56.         AVL_EDIT_WINDOW_PTR w;
  57.         w = &avl_windows[avl_window];
  58.         temp = AVL_MAKE_LINE("### DUMMY ###",0);
  59.         AVL_LINE_INSERT(temp,&head);
  60.         x = AVL_LOAD(fn, &head);
  61.         if (head == head -> next)  {
  62.             temp = AVL_MAKE_LINE("",2);
  63.             AVL_LINK(temp,head);
  64.             }
  65.         strcpy(avl_windows[avl_window].file_name,fn);
  66.         AVL_INIT_WINDOW(&avl_windows[avl_window], head -> next);
  67.         AVL_CURSOR_HOME();
  68.         AVL_UPDATE_SCREEN();
  69. }
  70.  
  71.  
  72. int AVL_LOAD(char *filen, AVL_LINE_PTR *at)
  73. {
  74.     AVL_WIN_PTR win;
  75.     AVL_LINE_PTR temp, temp2 = NULL;
  76.     int fp, i;
  77.     int line_no = 0;
  78.     long fsize;
  79.     char *buf = NULL;
  80.     char garbage = ' ';
  81.     char line[1024];
  82.     unsigned int size, n, j;
  83.     fp = open(filen,O_BINARY | O_RDWR, S_IREAD | S_IWRITE);
  84.     if (fp == -1) return 0;
  85.     sprintf(line,"Reading %s ...", filen);
  86.     win = AVL_MAKE_WINDOW("",23,1,25,strlen(line)+3,avl_wnd_bk_color,avl_wnd_color);
  87.     _setbkcolor(avl_msg_bk_color);
  88.     _settextcolor(avl_msg_color);
  89.     _settextposition(1,1);
  90.     _outtext(line);
  91.     lseek(fp,0L,2);
  92.     fsize = tell(fp);
  93.     if (fsize > 141000L)  {
  94.         AVL_ERROR("Source file too long (> 141,000 characters)!");
  95.         AVL_DEL_WINDOW(win);
  96.         close(fp);
  97.         return 0;
  98.         }
  99.     avl_windows[avl_window].buffer_size = fsize;
  100.     lseek(fp,0L,0);
  101.     size = fsize;
  102.     if (size == 0)
  103.         size = 10;
  104.     if ((buf = malloc(size)) == NULL)    {
  105.         AVL_ERROR("Out of memory to store file.");
  106.         AVL_DEL_WINDOW(win);
  107.         close(fp);
  108.         return -1;
  109.         }
  110.     if (fsize == 0L)
  111.         strcpy(buf,"   ");
  112.     while (fsize > 0L)  {
  113.         n = read(fp,buf,size);
  114.         if (n != size) {
  115.             AVL_ERROR("Can't read file.");
  116.             AVL_DEL_WINDOW(win);
  117.             close(fp);
  118.             return(0);
  119.             }
  120.         j = 0;
  121.         while (j < n)  {
  122.             for(i = 0; i < avl_start_pos; ++i)
  123.                 line[i] = ' ';
  124.             for (; !((buf[j] == 13) || (buf[j] == 10) || (j >= n)); ++i, ++j)
  125.                 line[i] = buf[j];
  126.             if ((buf[j] == 13) && (buf[j + 1] == 10)) ++j;
  127.             line[i] = '\0';
  128.             ++j;
  129.             temp = AVL_MAKE_LINE(line,line_no);
  130.             AVL_LINE_INSERT(temp, (temp2 == NULL) ? at : &temp2);
  131.             temp2 = temp;
  132.             }
  133.         fsize -= n;
  134.         }
  135.     if (buf[n-1] == '\n' || buf[n-1] == 10)  {
  136.         temp = AVL_MAKE_LINE("",line_no);
  137.         AVL_LINE_INSERT(temp, (temp2 == NULL) ? at : &temp2);
  138.         }
  139.     close(fp);
  140.     if (buf != NULL)
  141.         free(buf);
  142.     AVL_DEL_WINDOW(win);
  143.     return 1;
  144. }
  145.  
  146.  
  147.